<?php
/**
 * Created by PhpStorm
 * User: Jhinwy
 * Date: 6/20/24
 * Time: 1:44 PM
 */

namespace App\Services;

use App\Models\WechatTitleGalleryCategory;

class TitleGalleryCategoryService
{

    /**
     * 获取分类列表
     *
     * @param $params
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getList($params)
    {
        $pageSize = $params['page_size'] ?? 50;

        $list = WechatTitleGalleryCategory::query()->paginate($pageSize);

        return $list;
    }


    /**
     * 获取所有分类
     *
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getAll()
    {
        return WechatTitleGalleryCategory::query()->select('id', 'category_name')->get();
    }


    /**
     * 更新分类
     *
     * @param $params
     * @return bool
     */
    public function save($params)
    {
        $id   = $params['id'] ?? 0;
        $name = $params['name'] ?? '';
        if (!$name) {

            return false;
        }

        if ($id) {
            $category                = WechatTitleGalleryCategory::query()->find($id);
            $category->category_name = $name;

            return $category->save();
        } else {
            $category                = new WechatTitleGalleryCategory;
            $category->category_name = $name;
            $category->admin_id      = request()->admin->id;

            return $category->save();
        }
    }



    /**
     * 删除分类
     *
     * @param $id
     * @return bool|mixed|null
     */
    public function delete($id)
    {
        $category = WechatTitleGalleryCategory::query()->find($id);

        return $category->delete();
    }
}
